package com.zmcc.clients;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zmcc.entity.*;
import com.zmcc.entity.common.CommonRequest;
import com.zmcc.entity.common.CommonResponse;
import com.zmcc.sender.DefaultSyncClient;
import com.zmcc.sender.ISyncClient;
import com.zmcc.service.IVideoMsgRequest;
import com.zmcc.service.impl.VideoMsgRequestCommercial;
import com.zmcc.utils.WrapperUtils;

import java.util.List;

public class VideoMsgClient {

    private final IVideoMsgRequest requestWrapper;

    private final ISyncClient syncClient;

    private final String appkey;

    private final String appsecret;

    private final String mode;

    public VideoMsgClient(VideoMsgClient.Builder builder) {
        this.appkey = builder.appkey;
        this.appsecret = builder.appsecret;
        this.mode = builder.mode;
        this.syncClient = DefaultSyncClient.getInstance();
        this.requestWrapper = VideoMsgRequestCommercial.getInstance();
    }

    public static class Builder {
        private String appkey;
        private String appsecret;
        private String mode;

        public Builder() {

        }

        public VideoMsgClient.Builder appkey(String appkey) {
            this.appkey = appkey;
            return this;
        }

        public VideoMsgClient.Builder appsecret(String appsecret) {
            this.appsecret = appsecret;
            return this;
        }

        public VideoMsgClient.Builder mode(String mode) {
            this.mode = mode;
            return this;
        }

        public VideoMsgClient build() {
            return new VideoMsgClient(this);
        }
    }

    public String templateAddSend(VideoMsgTemplate template) {
        CommonRequest request = this.requestWrapper.getTemplateAddSendRequest(template);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response =  this.syncClient.fetch(request);
        String templateId = response.getData();
        return templateId;
    }

    public VideoMsgTemplateDTO templateStatusQuery(String templateId) {
        CommonRequest request = this.requestWrapper.getTemplateStatusQueryRequest(templateId);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response =  this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), VideoMsgTemplateDTO.class);
    }

    public String varFileAddSend(String templateId,String fileContent) {
        CommonRequest request = this.requestWrapper.getVarFileAddSendRequest(templateId,fileContent);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response =  this.syncClient.fetch(request);
        String varFileId = response.getData();
        return varFileId;
    }

    public VarFile varFileStatusQuery(String templateId, String varFileId) {
        CommonRequest request = this.requestWrapper.getVarFileStatusQueryRequest(templateId,varFileId);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response =  this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), VarFile.class);
    }

    public VideoMsgResult send(VideoMsgSendTemplate template) {
        CommonRequest request = this.requestWrapper.getSendVideoMsgRequest(template);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response =  this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), VideoMsgResult.class);
    }

    public VideoMsgResult deliveryStatus(String msgId,List<Mobile> mobiles) {
        CommonRequest request = this.requestWrapper.getDeliveryStatusRequest(msgId,mobiles);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response =  this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), VideoMsgResult.class);
    }

    public void subscribe(String flg,String san,String smsURL) {
        CommonRequest request = this.requestWrapper.getSubscribeRequest(flg,san,smsURL);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        this.syncClient.fetch(request);
    }

    public SmsSignatureResult addSignnature(List<Signature> signatureList ) {
        CommonRequest request = this.requestWrapper.getAddSignnatureRequest(signatureList);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response =  this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), SmsSignatureResult.class);
    }

    public SmsSignatureResult delSignnature(List<SignatureElement> signatureList) {
        CommonRequest request = this.requestWrapper.getDelSignnatureRequest(signatureList);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response =  this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), SmsSignatureResult.class);
    }
}
